<script setup name="Online" lang="ts">
import { parseTime } from '../../../utils/ruoyi.ts'
import { forceLogout, list as initData } from '@/api/monitor/online'
import type { OnlineQuery, OnlineVO } from '@/api/monitor/online/types'

const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_device_type } = toRefs<any>(proxy?.useDict('sys_device_type'))

const onlineList = ref<OnlineVO[]>([])
const loading = ref(true)
const total = ref(0)
const modal = useModal()
const message = useMessage()
const queryFormRef = ref()

const queryParams = ref<OnlineQuery>({
  pageNum: 1,
  pageSize: 10,
  ipaddr: '',
  userName: '',
})

/** 查询登录日志列表 */
async function getList() {
  loading.value = true
  const res = await initData(queryParams.value)
  onlineList.value = res.rows
  total.value = res.total
  loading.value = false
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields()
  queryParams.value.userName = undefined
  queryParams.value.ipaddr = undefined
  handleQuery()
}
/** 强退按钮操作 */
async function handleForceLogout(row: OnlineVO) {
  modal.confirm({
    title: `是否确认强退名称为"${row.userName}"的用户?`,
    onOk: async () => {
      await forceLogout(row.tokenId)
      await getList()
      message.success('删除成功')
    },
  })
}

onMounted(() => {
  getList()
})
</script>

<template>
  <div class="p-2">
    <a-card class="mb-20px">
      <a-form ref="queryFormRef" :model="queryParams" layout="inline" class="inline-form">
        <a-form-item title="登录地址" data-index="ipaddr">
          <a-input v-model:value="queryParams.ipaddr" placeholder="请输入登录地址" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </a-form-item>
        <a-form-item title="用户名称" data-index="userName">
          <a-input v-model:value="queryParams.userName" placeholder="请输入用户名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </a-form-item>
        <a-form-item>
          <a-space>
            <a-button type="primary" @click="handleQuery">
              搜索
            </a-button>
            <a-button @click="resetQuery">
              重置
            </a-button>
          </a-space>
        </a-form-item>
      </a-form>
    </a-card>
    <a-card shadow="hover">
      <a-table
        v-loading="loading"
        :pagination="false"
        :data-source="onlineList.slice((queryParams.pageNum - 1) * queryParams.pageSize, queryParams.pageNum * queryParams.pageSize)"
        style="width: 100%;"
      >
        <a-table-column title="序号" width="50" data-index="index" align="center">
          <template #default="{ index }">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + index + 1 }}</span>
          </template>
        </a-table-column>
        <a-table-column title="会话编号" align="center" data-index="tokenId" :ellipsis="true" />
        <a-table-column title="登录名称" align="center" data-index="userName" :show-overflow-tooltip="true" />
        <a-table-column title="客户端" align="center" data-index="clientKey" :show-overflow-tooltip="true" />
        <a-table-column title="设备类型" align="center">
          <template #default="{ record }">
            <bh-dict-tag :options="sys_device_type" :value="record.deviceType" />
          </template>
        </a-table-column>
        <a-table-column title="所属部门" align="center" data-index="deptName" :show-overflow-tooltip="true" />
        <a-table-column title="主机" align="center" data-index="ipaddr" :show-overflow-tooltip="true" />
        <a-table-column title="登录地点" align="center" data-index="loginLocation" :show-overflow-tooltip="true" />
        <a-table-column title="操作系统" align="center" data-index="os" :show-overflow-tooltip="true" />
        <a-table-column title="浏览器" align="center" data-index="browser" :show-overflow-tooltip="true" />
        <a-table-column title="登录时间" align="center" data-index="loginTime" width="180">
          <template #default="{ record }">
            <span>{{ parseTime(record.loginTime) }}</span>
          </template>
        </a-table-column>
        <a-table-column title="操作" align="center" class-name="small-padding fixed-width">
          <template #default="{ record }">
            <a-button v-hasPermi="['monitor:online:forceLogout']" type="link" @click="handleForceLogout(record)">
              强退
            </a-button>
          </template>
        </a-table-column>
      </a-table>

      <div class="text-right mt-5">
        <a-pagination
          v-show="total > 0"
          v-model:current="queryParams.pageNum" v-model:pageSize="queryParams.pageSize" :total="total"
          show-less-items
        />
      </div>
    </a-card>
  </div>
</template>
